package com.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.entity.PeisongxinxiEntity;
import com.entity.QishouEntity;
import com.service.PeisongxinxiService;
import com.service.QishouService;
import com.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;
import com.annotation.SysLog;

import com.entity.PeisongdingdanEntity;
import com.entity.view.PeisongdingdanView;

import com.service.PeisongdingdanService;
import com.service.TokenService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MPUtil;
import com.utils.MapUtils;
import com.utils.CommonUtil;
import java.io.IOException;

/**
 * 配送订单
 * 后端接口
 * @author 
 * @email 
 * @date 2024-04-18 09:34:30
 */
@RestController
@RequestMapping("/peisongdingdan")
public class PeisongdingdanController {
    @Autowired
    private PeisongdingdanService peisongdingdanService;

    @Autowired private PeisongxinxiService peisongxinxiService;

    @Autowired
    private QishouService qishouService;



    /**
     * 后台列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params,PeisongdingdanEntity peisongdingdan,
		HttpServletRequest request){
		String tableName = request.getSession().getAttribute("tableName").toString();
		if(tableName.equals("shangjia")) {
			peisongdingdan.setShangjiazhanghao((String)request.getSession().getAttribute("username"));
		}
		if(tableName.equals("yonghu")) {
			peisongdingdan.setYonghuzhanghao((String)request.getSession().getAttribute("username"));
		}
        EntityWrapper<PeisongdingdanEntity> ew = new EntityWrapper<PeisongdingdanEntity>();

		PageUtils page = peisongdingdanService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, peisongdingdan), params), params));

        return R.ok().put("data", page);
    }
    
    /**
     * 前台列表
     */
	@IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,PeisongdingdanEntity peisongdingdan, 
		HttpServletRequest request){
        EntityWrapper<PeisongdingdanEntity> ew = new EntityWrapper<PeisongdingdanEntity>();

		PageUtils page = peisongdingdanService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, peisongdingdan), params), params));
        return R.ok().put("data", page);
    }



	/**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( PeisongdingdanEntity peisongdingdan){
       	EntityWrapper<PeisongdingdanEntity> ew = new EntityWrapper<PeisongdingdanEntity>();
      	ew.allEq(MPUtil.allEQMapPre( peisongdingdan, "peisongdingdan")); 
        return R.ok().put("data", peisongdingdanService.selectListView(ew));
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(PeisongdingdanEntity peisongdingdan){
        EntityWrapper< PeisongdingdanEntity> ew = new EntityWrapper< PeisongdingdanEntity>();
 		ew.allEq(MPUtil.allEQMapPre( peisongdingdan, "peisongdingdan")); 
		PeisongdingdanView peisongdingdanView =  peisongdingdanService.selectView(ew);
		return R.ok("查询配送订单成功").put("data", peisongdingdanView);
    }
	
    /**
     * 后台详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        PeisongdingdanEntity peisongdingdan = peisongdingdanService.selectById(id);
        return R.ok().put("data", peisongdingdan);
    }

    /**
     * 前台详情
     */
	@IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        PeisongdingdanEntity peisongdingdan = peisongdingdanService.selectById(id);
        return R.ok().put("data", peisongdingdan);
    }
    



    /**
     * 后台保存
     */
    @RequestMapping("/save")
    @SysLog("新增配送订单") 
    public R save(@RequestBody PeisongdingdanEntity peisongdingdan, HttpServletRequest request){
    	//ValidatorUtils.validateEntity(peisongdingdan);
        peisongdingdanService.insert(peisongdingdan);
        return R.ok();
    }
    
    /**
     * 前台保存
     */
    @SysLog("新增配送订单")
    @RequestMapping("/add")
    public R add(@RequestBody PeisongdingdanEntity peisongdingdan, HttpServletRequest request){
    	//ValidatorUtils.validateEntity(peisongdingdan);
        peisongdingdanService.insert(peisongdingdan);
        return R.ok();
    }





    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    @SysLog("修改配送订单")
    public R update(@RequestBody PeisongdingdanEntity peisongdingdan, HttpServletRequest request){
        //ValidatorUtils.validateEntity(peisongdingdan);
        peisongdingdanService.updateById(peisongdingdan);//全部更新
        return R.ok();
    }



    /**
     * 修改立即接单 将接单状态改为配送中
     */
    @RequestMapping("/updateStatus")
    @Transactional
    @SysLog("修改配送订单")
    public R updateInfo(@RequestBody Map<String,Object>map,HttpServletRequest request){

        try {
            Long id = (Long)request.getSession().getAttribute("userId");
            QishouEntity u = qishouService.selectById(id);
            //根据id查询出订单信息；
            PeisongdingdanEntity peisongdingdan=peisongdingdanService.getById(map);

            //插入配送信息
            PeisongxinxiEntity peisongxinxi=new PeisongxinxiEntity();
            peisongxinxi.setAddtime(new Date());
            peisongxinxi.setDingdanbianhao(peisongdingdan.getDingdanbianhao());
            peisongxinxi.setShangpinmingcheng(peisongdingdan.getShangpinmingcheng());
            peisongxinxi.setShangpinfenlei(peisongdingdan.getShangpinfenlei());
            peisongxinxi.setShangjiazhanghao(peisongdingdan.getShangjiazhanghao());
            peisongxinxi.setShangpinzhaopian(peisongdingdan.getShangpinzhaopian());
            peisongxinxi.setYonghuzhanghao(peisongdingdan.getYonghuzhanghao());
            peisongxinxi.setYonghuxingming(peisongdingdan.getYonghuxingming());
            peisongxinxi.setPeisongdizhi(peisongdingdan.getPeisongdizhi());
            peisongxinxi.setPeisongshijian(new Date());
            peisongxinxi.setPeisongzhuangtai("配送中");
            peisongxinxi.setQishouzhanghao(u.getQishouzhanghao());
            peisongxinxi.setQishouxingming(u.getQishouxingming());
            peisongxinxiService.insert(peisongxinxi);

            //修改订单状态为已接单
            peisongdingdanService.updateStatus(map);
        }catch (Exception e){
            return R.error("修改失败");
        }


        return R.ok();
    }

    

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @SysLog("删除配送订单")
    public R delete(@RequestBody Long[] ids){
        peisongdingdanService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
    
	










}
